'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async appointcreate() {
    const { ctx } = this;
    const { aptheme, aprequirement, apTime, apAdress, apType, uid, pictureurl } = ctx.request.body;
    if (!(aptheme && aprequirement && apTime && apAdress && apType && uid && pictureurl)) {
      return ctx.body = { success: false, msg: '请填写必要内容' };
    }
    try {
      const res = await ctx.model.Appointment.create({
        aptheme,
        aprequirement,
        apTime,
        apAdress,
        apType,
        uid,
        addTime: Date.now(),
        browse: 0,
      });
      const newurl = pictureurl.split(',');
      for (const item of newurl) {
        await ctx.model.Picture.create({
          appointmentid: res.appointmentid,
          photourl: item,
        });
      }
      ctx.body = { success: true, msg: '发布成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '发布失败' };
    }
  }

  async appointupdate() {
    const { ctx } = this;
    const { appointmentid, aptheme, aprequirement, apTime, apAdress, apType, browse } = ctx.request.body;
    if (!appointmentid) {
      return ctx.body = { success: false, msg: '请传入id' };
    }
    const update = {};
    if (aptheme) {
      update.aptheme = aptheme;
    }
    if (aprequirement) {
      update.aprequirement = aprequirement;
    }
    if (apTime) {
      update.apTime = apTime;
    }
    if (browse) {
      update.browse = browse;
    }
    if (apAdress) {
      update.apAdress = apAdress;
    }
    if (apType) {
      update.apType = apType;
    }
    try {
      await ctx.model.Appointment.update(
        update,
        {
          where: {
            appointmentid,
          },
        }
      );
      ctx.body = { success: true, msg: '更新成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '更新失败' };
    }
  }

  async appointdelete() {
    const { ctx } = this;
    const { appointmentid } = ctx.request.body;
    if (!appointmentid) {
      return ctx.body = { success: false, msg: '请传入id' };
    }
    try {
      await ctx.model.Appointment.destroy({
        where: {
          appointmentid,
        },
      });
      ctx.body = { success: true, msg: '删除成功' };
    } catch (e) {
      ctx.body = { success: false, msg: '删除失败' };
    }
  }

  async appointfind() {
    const { ctx } = this;
    const { appointmentid, aptheme, apTime, apAdress, apType, uid, page } = ctx.request.body;
    const where = {};
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    if (aptheme) {
      where.aptheme = aptheme;
    }
    if (uid) {
      where.uid = uid;
    }
    if (apTime) {
      where.apTime = apTime;
    }
    if (apAdress) {
      where.apAdress = apAdress;
    }
    if (apType) {
      where.apType = apType;
    }
    if (appointmentid) {
      where.appointmentid = appointmentid;
    }
    try {
      await ctx.model.Appointment.hasMany(ctx.model.Thumbs, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Comment, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Collection, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Picture, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      const res = await ctx.model.Appointment.findAndCountAll({
        where,
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'appointmentid', 'DESC' ]],
      });
      const length = await ctx.model.Appointment.findAll({
        where,
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }


  async appointlike() {
    const { ctx, app } = this;
    const { Op } = app.Sequelize;
    const { search, page } = ctx.request.body;
    const limit = 10;
    const offset = (page - 1) * limit || 0;
    try {
      await ctx.model.Appointment.hasMany(ctx.model.Thumbs, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Comment, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Collection, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      await ctx.model.Appointment.hasOne(ctx.model.User, {
        foreignKey: 'uid',
        sourceKey: 'uid',
      });
      await ctx.model.Appointment.hasMany(ctx.model.Picture, {
        foreignKey: 'appointmentid',
        sourceKey: 'appointmentid',
      });
      const res = await ctx.model.Appointment.findAndCountAll({
        where: {
          [Op.or]: [
            {
              apAdress: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              aptheme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              apType: {
                [Op.like]: `%${search}%`,
              },
            },
          ],

        },
        limit,
        offset,
        include: [{
          model: ctx.model.Picture,
        }, {
          model: ctx.model.Thumbs,
        }, {
          model: ctx.model.Collection,
        }, {
          model: ctx.model.Comment,
        }, {
          model: ctx.model.User,
          attributes: {
            exclude: [ 'pas' ],
          },
        }],
        order: [[ 'appointmentid', 'DESC' ]],
      });
      const length = await ctx.model.Appointment.findAll({
        where: {
          [Op.or]: [
            {
              apAdress: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              aptheme: {
                [Op.like]: `%${search}%`,
              },
            },
            {
              apType: {
                [Op.like]: `%${search}%`,
              },
            },
          ],
        },
      });
      ctx.body = { success: true, data: res, length: length.length, maxpage: Math.ceil(length.length / limit) };
    } catch (e) {
      ctx.body = { success: false, msg: '查询失败' };
    }
  }
}

module.exports = HomeController;
